using System;
using System.Web.Mvc;
using EventHandler.Libraries.Business;
using EventHandler.Libraries.Business.Database;
using EventHandler.Libraries.Business.Security;
using EventHandler.UI.Models;

namespace EventHandler.UI.Controllers
{
    public class AuthenticationController : Controller
    {
        private IDatabaseHelper _dbhelper;

        public AuthenticationController()
        {
            _dbhelper = new DatabaseHelper();
        }

        public AuthenticationController(IDatabaseHelper dbhelper)
        {
            _dbhelper = dbhelper;
        }

        public JsonResult LoggedIn()
        {
            return Json(Session["login"]);
        }

        public JsonResult Authenticate(string username, string password)
        {
            string message = username + " " + password;
            bool success = username == "test" && password == "test";
            addToken();
            return Json(new { Success = success, Message = message });
        }

        public JsonResult CreateAccount(string username, string firstName, string lastName, string email, string password)
        {
            AccountHelper helper = new AccountHelper(_dbhelper);
            IAccount account = null;
            Account acc = new Account
                            {
                                FirstName = firstName,
                                LastName = lastName,
                                Username = username,
                                Email = email,
                                Password = password,
                                AuthenticationTypeId = (int) AuthencationType.EventHandler
                            };
            
            try
            {
                account = helper.CreateAccount(acc);
                addToken();
            }
            catch(Exception ex)
            {
                revokeToken();
                return Json(new {Success = false, ex.Message});
            }

            return Json(new {Success = true, Account = account});
        }

        public ActionResult LogOut()
        {
            revokeToken();
            return Redirect("/");
        }

        public JsonResult validateName(string firstName, string lastName)
        {
            bool valid = false;
            string message = "";

            return Json(new {Valid = valid, Message = message});
        }

        public JsonResult validatePassword(string password, string passwordConfirm)
        {
            bool valid = false;
            string message = "";

            return Json(new { Valid = valid, Message = message });
        }

        public JsonResult validateEmail(string email, string emailConfirm)
        {
            bool valid = false;
            string message = "";

            return Json(new { Valid = valid, Message = message });
        }

        public JsonResult validateUsername(string username)
        {
            bool valid = false;
            string message = "";

            return Json(new { Valid = valid, Message = message });
        }

        public void addToken()
        {
            if (Session["login"] == null)
            {
                Session.Add("login", true);
            }
            else
            {
                Session["login"] = true;
            }
        }

        public void revokeToken()
        {
            if (Session["login"] == null)
            {
                Session.Add("login", false);
            }
            else
            {
                Session["login"] = false;
            }
            
        }
    }
}
